package com.work.flow.controller;

import com.work.flow.common.R;
import com.work.flow.model.PageModel;
import com.work.flow.model.StoryBoard;
import com.work.flow.service.StoryBoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/storyboard/api/v1")
@RestController
public class StoryBoardController {
    @Autowired
    StoryBoardService storyBoardService;


    @PostMapping("/merge")
    public R merge(@RequestBody StoryBoard storyBoard) {
        try {
            return R.data(storyBoardService.merge(storyBoard));
        } catch (Exception e) {
            return R.error();
        }
    }

    @PostMapping("/start/{id}")
    public R startTask(@RequestBody @PathVariable(value = "id", required = true) String id) {
        try {
            storyBoardService.startTask(id);
            return R.ok();
        } catch (Exception e) {
            return R.error();
        }
    }

    /**
     * 删除模型
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable(value = "id", required = true) String id){
        try {
            storyBoardService.deleteById(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }


    @GetMapping("/getCount")
    public R getCount(@RequestParam(required = false) String key){
        try {
            return R.data(storyBoardService.getCount(key));
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }

    @PostMapping("/queryByPage")
    public R queryByPage(@RequestBody PageModel pageModel){
        try {
            return R.data(storyBoardService.queryByPage(pageModel));
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }
    @GetMapping("/detail/{id}")
    public R getById(@PathVariable(value = "id", required = true) String id){
        try {
            return R.data(storyBoardService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.error(e.getMessage());
        }
    }
}
